#!/bin/bash

#get current date and time

year=`date -d "today" +"%Y"`;
month=`date -d "today" +"%m"`;
day=`date -d "today" +"%d"`;
hour=`date -d "today" +"%k"`;

time=$(date +%Y"."%m"."%d" "%k":"%M":"%S);
#logFile
logFile="/var/backup/log/backup_${year}${month}.log";
#echo $logFile;
if [ ! -f "$logFile" ]
then
                touch "$logFile";
fi


echo $time backup starting.....  >> $logFile;

#format time and date
month=$[$[$month*10]/10];
day=$[$[$day*10]/10];

# test date
#year=2016;month=3;day=1;hour=0;

# time an hour ago
hour=$[$[$[$hour*10]/10]-1];

if [ $hour -lt 0 ];then
        echo $time A Branch New day $year-$month-$day, backup files yestoday on the last hour.  >> $logFile;
        hour=23;
        day=$[$day-1];
        if [ $day -eq 0 ];then
                echo $time A Branch New Month $year-$month, backup files last Month on the last hour.  >> $logFile;
                month=$[$month-1];

                if [ $month -eq 0 ];then
                        echo $time A Branch New Year $year, backup files last year on the last hour.  >> $logFile;
                        day=31;
                        month=12;
                        year=$[$year-1];
                else
                        if [ $month -eq 1 ] || [ $month -eq 3 ] || [ $month -eq 5 ] || [ $month -eq 7 ] || [ $month -eq 8 ] || [ $month -eq 10 ];then
                                day=31;
                        fi

                        if [ $month -eq 4 ] || [ $month -eq 6 ] || [ $month -eq 9 ] || [ $month -eq 11 ];then
                                day=30;
                        fi

                        if [ $month -eq 2 ];then
                                if [ $[$year/4] -eq 0 ];then
                                        day=29;
                                else
                                        day=28;
                                fi
                        fi
                fi
        fi
fi

date_time=$year/$month/$day/$hour;

echo $time $date_time  >> $logFile;

file_path='/var2/download1/download';
bak_path='/var/backup/download1/download';


arr=(avatar ErrLog feedback homework im notice);

for i in ${arr[@]}
do
        thisPath=$file_path/$i/$date_time;#current Path
        desPath=$bak_path/$i/$date_time;#destination Path
        desYearPath=$bak_path/$i/$year;
        desMonthPath=$bak_path/$i/$year/$month;
        desDayPath=$bak_path/$i/$year/$month/$day;
#       echo $thisPath;

        if [ -d "$thisPath" ];
        then
                echo $time ++[$i]=$thisPath >> $logFile;
                if [ ! -d "$desYearPath" ];
                then
                #       #echo "$desPath not exist..";
                mkdir "$desYearPath";
                fi
                if [ ! -d "$desMonthPath" ];
                then
                #       echo "mk m  == $desMonthPath";
                        mkdir "$desMonthPath";
                fi
                if [ ! -d "$desDayPath" ];
                then
                #       echo "mk d == $desDayPath";
                        mkdir "$desDayPath";
                fi

                echo $time total files:$(ls -d ${thisPath}  -l | grep "^-" | wc -l) >> $logFile;
                cp -r "$thisPath/" "$desDayPath/";
        else
                echo $time --[$i] has no fresh files for backup... >> $logFile;
        fi
done

echo $time end============================== >> $logFile;